# Maintainer: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=libexodus
_realname2=seacas
_releases=2021-05-12
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=8.11
pkgrel=4
pkgdesc="EXODUS is a model developed to store and retrieve data for finite element analyses (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
conflicts=("${MINGW_PACKAGE_PREFIX}-seacas"
           "${MINGW_PACKAGE_PREFIX}-trilinos")
depends=("${MINGW_PACKAGE_PREFIX}-crt" "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
makedepends=("unzip"
             "${MINGW_PACKAGE_PREFIX}-netcdf"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake")
url="https://gsjaardema.github.io/seacas/"
license=('BSD')
source=("https://github.com/gsjaardema/seacas/archive/v${_releases}.zip"
        "cmake-fix.patch"
        "netcdf-4.9.0-fix.patch")
sha256sums=('818f9eff35a4fae682cf3b81897d49736c332cb3549ae2b2fc22678b21ee06ee'
            '5248fe9045d203770eb18510b4746c47e10d269db691adc98f43aa419c2b9aa4'
            'c88343e7c943fdd1e8f1d0969b8a3b61b02cbdae936884f66ea97352e0a3219b')
noextract=("v${_releases}.zip") # bsdtar could not unpack this archive

prepare() {  
  rm -rf "${srcdir}/seacas-${_releases}"
  unzip -oq v${_releases}.zip -d ${srcdir}
  cd ${_realname2}-${_releases}
  sed -i 's:-pedantic:/:g' cmake-exodus

  patch -Np1 -i "${srcdir}/cmake-fix.patch"
  # Patch for netcdf-4.9.0 based on
  # https://github.com/sandialabs/seacas/commit/5e1b19181110c52b76d13e3f60da2c8cf678c941:
  patch -Np1 -i "${srcdir}/netcdf-4.9.0-fix.patch"
}

build() {
  [[ -d "${_realname2}-${_releases}/build-${MINGW_CHOST}" ]] && rm -rf "${_realname2}-${_releases}/build-${MINGW_CHOST}"
  mkdir -p "${_realname2}-${_releases}/build-${MINGW_CHOST}" && cd "${_realname2}-${_releases}/build-${MINGW_CHOST}"
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      INSTALL_PATH=${MINGW_PREFIX} THREADSAFE=ON ../cmake-exodus \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${_realname2}-${_releases}/build-${MINGW_CHOST}"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 "${srcdir}/seacas-${_releases}/packages/seacas/libraries/exodus"/COPYRIGHT ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
